use "D:\1.Articles\Datasets\Dataset_WVS\大陆和港澳台\WVS_Wave_7_Macau_SAR_Stata_v5.0.dta", clear

recode Q260 -5=. 2=0, gen(Gender)
recode Q262 -5=., gen(Age)
recode Q275 -5=. -1=., gen(Education)
recode Q273 -5=. 2=0 3=0 4=0 5=0 6=0,gen(MaritalStatus)
recode Q263 2=0 , gen(immigrationstatus)

recode Q49 -5=. , gen(lifesa)
recode Q46 4=1 2=3 3=2 1=4,gen(happiness)
gen swb = happiness+lifesa

recode Q288 -5=. , gen(relativeincome)

recode Q287 -5=. 1=5 2=4 4=2 5=1 , gen(socialclass)

recode Q57 -5=. 2=0 , gen(Generalized_trust)

recode Q48 -5=., gen(Control_perceptions)

recode Q47 -5=. 1=5 2=4 4=2 5=1, gen(health)

recode Q106 -5=., gen(Attitudeincomeinequality)

generate AgeDi = 1
replace AgeDi = 0 if Age <= 35

gen EduDi = 1
replace EduDi = 0 if Education <= 3

drop if missing(relativeincome) | missing(swb) | missing(Generalized_trust) | missing(Control_perceptions) | missing(Attitudeincomeinequality) | missing(Gender) | missing(Age) | missing(Education) | missing(MaritalStatus) | missing(socialclass)

sum happiness lifesa relativeincome Attitudeincomeinequality Control_perceptions Generalized_trust Gender Age Education MaritalStatus immigrationstatus   

pwcorr swb relativeincome Attitudeincomeinequality Generalized_trust Control_perceptions Gender Age Education MaritalStatus immigrationstatus, sig

*Basic model
reg swb relativeincome
outreg2 using D:\res.xls, replace bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb relativeincome Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\res.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

*Robustness check
reg swb socialclass Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\res.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)
reg health relativeincome Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\res.xls, append bdec(3) tdec(3) ctitle(health) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

sum relativeincome, d
gen p5 = r(p5)
gen p95 = r(p95)
gen trimmed_relativeincome5 = relativeincome if relativeincome > r(p5) & relativeincome < r(p95)
reg swb trimmed_relativeincome5 Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\res.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

drop  p5  p95  trimmed_relativeincome5

*Mediation

reg Attitudeincomeinequality relativeincome Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, append bdec(3) tdec(3) ctitle(acceptance of income inequality) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb relativeincome Attitudeincomeinequality Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg Generalized_trust relativeincome Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, replace bdec(3) tdec(3) ctitle(trust) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb relativeincome Generalized_trust Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg Control_perceptions relativeincome Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, append bdec(3) tdec(3) ctitle(control perceptions) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb relativeincome Control_perceptions Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\mediation.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

*Heterogeneous test

reg swb c.relativeincome#i.Gender Age Education i.MaritalStatus immigrationstatus
outreg2 using D:\heterogeneity.xls, replace bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb c.relativeincome#i.MaritalStatus Age Education i.Gender immigrationstatus
outreg2 using D:\heterogeneity.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb c.relativeincome#i.AgeDi i.MaritalStatus  Education i.Gender immigrationstatus
outreg2 using D:\heterogeneity.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb c.relativeincome#i.EduDi i.MaritalStatus Age  i.Gender immigrationstatus
outreg2 using D:\heterogeneity.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

reg swb c.relativeincome#i.immigrationstatus i.MaritalStatus Age  i.Gender Education
outreg2 using D:\heterogeneity.xls, append bdec(3) tdec(3) ctitle(swb) e(r2_a,F) addstat(F test,e(p)) alpha(0.001, 0.01, 0.05)

******************************************************
* Sobel Test: Mediator = Attitudeincomeinequality
******************************************************

* Step 1: Regression of mediator on independent variable
reg Attitudeincomeinequality relativeincome Gender Age Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local a  = _b[relativeincome]
local sa = _se[relativeincome]

* Step 2: Regression of dependent variable on IV + mediator
reg swb relativeincome Attitudeincomeinequality Gender Age Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local b  = _b[Attitudeincomeinequality]
local sb = _se[Attitudeincomeinequality]

* Step 3: Compute Sobel test statistic
local sobel_z = `a' * `b' / sqrt(`b'^2 * `sa'^2 + `a'^2 * `sb'^2)

* Display results
display "Sobel test Z-value: " `sobel_z'

* Step 4: Compute p-value
local p_value = 2 * (1 - normal(abs(`sobel_z')))
display "P-value: " `p_value'


******************************************************
* Sobel Test: Mediator = Generalized_trust
******************************************************

* Step 1: Regression of mediator on independent variable
reg Generalized_trust relativeincome Gender AgeDi Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local a  = _b[relativeincome]
local sa = _se[relativeincome]

* Step 2: Regression of dependent variable on IV + mediator
reg swb Generalized_trust relativeincome Gender AgeDi Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local b  = _b[Generalized_trust]
local sb = _se[Generalized_trust]

* Step 3: Compute Sobel test statistic
local sobel_z = `a' * `b' / sqrt(`b'^2 * `sa'^2 + `a'^2 * `sb'^2)

* Display results
display "Sobel test Z-value: " `sobel_z'

* Step 4: Compute p-value
local p_value = 2 * (1 - normal(abs(`sobel_z')))
display "P-value: " `p_value'


******************************************************
* Sobel Test: Mediator = Control_perceptions
******************************************************

* Step 1: Regression of mediator on independent variable
reg Control_perceptions relativeincome Gender Age Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local a  = _b[relativeincome]
local sa = _se[relativeincome]

* Step 2: Regression of dependent variable on IV + mediator
reg swb Control_perceptions relativeincome Gender Age Education i.MaritalStatus immigrationstatus

* Save coefficient and standard error
local b  = _b[Control_perceptions]
local sb = _se[Control_perceptions]

* Step 3: Compute Sobel test statistic
local sobel_z = `a' * `b' / sqrt(`b'^2 * `sa'^2 + `a'^2 * `sb'^2)

* Display results
display "Sobel test Z-value: " `sobel_z'

* Step 4: Compute p-value
local p_value = 2 * (1 - normal(abs(`sobel_z')))
display "P-value: " `p_value'
